<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicModal
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'physical:sportsPlan:edit'"
    okText="复制"
    @register="register"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPhysicalsportsPlanForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { SportsPlan, sportsPlanListCopy } from '/@/api/physical/sportsPlan';
  import moment from 'moment';
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('physical.sportsPlan');
  const { showMessage } = useMessage();
  const record = ref<SportsPlan>({} as SportsPlan);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('复制体育计划'),
  }));
  const yearShowOne = ref(false);
  const inputFormSchemas: FormSchema[] = [
    {
      label: t('开始学年'),
      field: 'startAdaptAcademicYear',
      component: 'DatePicker',
      componentProps: ({ formModel }) => {
        return {
          mode: 'year',
          allowClear: true,
          format: 'YYYY',
          onchange: (e) => {
            console.log(e);
          },
          onPanelChange: (value) => {
            formModel.startAdaptAcademicYear = moment(value).format('YYYY');
          },
        };
      },
    },
    {
      label: t('结束学年'),
      field: 'endAdaptAcademicYear',
      component: 'DatePicker',
      componentProps: ({ formModel }) => {
        return {
          mode: 'year',
          allowClear: true,
          format: 'YYYY',
          onchange: (e) => {
            console.log(e);
          },
          onPanelChange: (value) => {
            formModel.endAdaptAcademicYear = moment(value).format('YYYY');
          },
        };
      },
    },
  ];
  // function openChangeOne
  // function panelChangeOne
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });
  let planId;
  const [register, { closeModal, setModalProps }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ loading: true });
    planId = data.planId;
    setFieldsValue(record.value);
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    try {
      const data = await validate();
      console.log(data);
      setModalProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        triggerId: record.value.triggerId,
      };
      data.planId = planId;
      const res = await sportsPlanListCopy(params, data);
      showMessage(res.message);
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
